package edu.uit.se.slgroup.construct.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import edu.uit.se.slgroup.construct.domain.Article;
import edu.uit.se.slgroup.construct.domain.ArticleTemp;

public class ArticleDaoImpl implements ArticleDao{

	@Autowired
	SessionFactory sessionFactory;
	
	@Transactional
	public int insertArticle(Article article) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(article);
		tx.commit();
		Serializable id = session.getIdentifier(article);
		session.close();
		return (Integer) id;
	}

	public List<Article> getArticleList() {
		Session session = sessionFactory.openSession();
		@SuppressWarnings("unchecked")
		List<Article> articleList = session.createQuery("from Article").list();
		session.close();
		return articleList;
	}

	public Article getArticleById(int id) {
		Session session = sessionFactory.openSession();
		Article article = (Article) session.load(Article.class, id);
		//dong ket noi, nhieu truong hop khong lay duoc obj
		//session.close();
		return article;
	}

	public int updateArticle(Article article) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(article);
		tx.commit();
		Serializable id = session.getIdentifier(article);
		session.close();
		return (Integer) id;
	}

	public int deleteArticle(int id) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		Article article = (Article) session.load(Article.class, id);
		session.delete(article);
		tx.commit();
		Serializable ids = session.getIdentifier(article);
		session.close();
		return (Integer) ids;
	}

	public List<ArticleTemp> getArticleTempList() {
		Session session = sessionFactory.openSession();
		@SuppressWarnings("unchecked")
		
		List<ArticleTemp> articleTempList = new ArrayList<ArticleTemp>();
		String SQL_QUERY = "SELECT A.id, A.title, A.content, A.urlImage, S.name "
				+ " FROM edu.uit.se.slgroup.construct.domain.Article A, edu.uit.se.slgroup.construct.domain.SubCategory S" 
				+ " WHERE A.idSubCategory = S.id ORDER BY A.id ASC ";
		Query query = session.createQuery(SQL_QUERY);
		List result = query.list();
		Iterator itr = result.iterator();
		while(itr.hasNext()) {
			Object[] row = (Object[]) itr.next();
			ArticleTemp articleTemp = new ArticleTemp();
			articleTemp.setId((Integer) row[0]);
			articleTemp.setTitle((String) row[1]);
			articleTemp.setContent((String) row[2]);
			articleTemp.setUrlImage((String) row[3]);
			articleTemp.setIdSubCategory((String) row[4]);
			articleTempList.add(articleTemp);
		}
		
		for (ArticleTemp articleTemp : articleTempList) {
			System.out.println("Id : " + articleTemp.getId());
		}
		session.close();
		
		return articleTempList;
	}

}
